package com.example.userregist;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;

public class ShowActivity extends AppCompatActivity {

    private Button buttonBuy;
    private ProgressBar progressBar1;
    private ProgressBar progressBar2;
    private ProgressBar progressBar3;
    private Button button;
    private User user;
    //private TextView tv_hp;
    //private TextView tv_attack;
   //private TextView tv_speed;
    private String itemInfoStr;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);

        //1.获取开启此页面的意图对象
        Intent intent = this.getIntent();
        //2.根据key  取出相应的value值
         String name = intent.getStringExtra("name");
         String password = intent.getStringExtra("password");
        //3.把用户名和密码展示到控件上
        TextView tv_name = findViewById(R.id.tv_name);
        TextView tv_password = findViewById(R.id.tv_password);
        tv_name.setText(name);
        tv_password.setText(password);

        user = new User(name+"学号30",password);

        buttonBuy = findViewById(R.id.btn_baby);
        //武器界面
        buttonBuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(ShowActivity.this,ShopActivity.class);
                startActivityForResult(intent,1);
            }
        });
        button = findViewById(R.id.btn_return);
        //登陆界面
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent();
                intent.putExtra("User",user);
                setResult(RESULT_OK,intent);
                finish();
            }
        });

        progressBar1=findViewById(R.id.progressBar1);
        progressBar2=findViewById(R.id.progressBar2);
        progressBar3=findViewById(R.id.progressBar3);
        /*progressBar1.setProgress(10);
        progressBar2.setProgress(10);
        progressBar3.setProgress(10);*/
        readItemInfo();
        /*tv_hp = findViewById(R.id.tv_HP);
        tv_attack = findViewById(R.id.tv_attack);
        tv_speed = findViewById(R.id.tv_speed);*/

    }
    //实现进度条
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //ItemInfo itemInfo = (ItemInfo) data.getSerializableExtra("ItemInfo");
        itemInfoStr = data.getStringExtra("ItemInfo");
        Gson gson = new Gson();
        ItemInfo itemInfo = gson.fromJson(itemInfoStr,ItemInfo.class);
        progressBar1.setProgress(progressBar1.getProgress()+itemInfo.getHp());
        progressBar2.setProgress(progressBar2.getProgress()+itemInfo.getAttack());
        progressBar3.setProgress(progressBar3.getProgress()+itemInfo.getSpeed());
        saveItemInfo();

        //后面数字？怎样改变？？？
        /*tv_hp.setText("tv_hp.getText()");
        tv_attack.setText(itemInfo.getAttack());
        tv_speed.setText(itemInfo.getSpeed());*/
    }
    //返回第一页
    @Override
    public void onBackPressed() {
        Intent intent=new Intent();
        intent.putExtra("User",user);
        setResult(RESULT_OK,intent);
        finish();
    }
    //保存
    private void saveItemInfo(){

        //获取对象
        SharedPreferences sp = getSharedPreferences("ItemInfoState",MODE_PRIVATE);
        //获取编辑器
        SharedPreferences.Editor editor = sp.edit();
        Gson gson = new Gson();
        ItemInfo itemInfo = new ItemInfo(null,progressBar1.getProgress(),progressBar1.getProgress(),progressBar3.getProgress());
        //存入数据
        editor.putString("ItemInfoState",gson.toJson(itemInfo));
        //保存
        editor.apply();
        //弹出提示信息
        Toast.makeText(this,"装备属性保存成功！",Toast.LENGTH_SHORT).show();
    }
    //读取
    private void readItemInfo(){

        //获取对象
        SharedPreferences sp = getSharedPreferences("ItemInfoState",MODE_PRIVATE);
        itemInfoStr = sp.getString("ItemInfoState",null);
        if (itemInfoStr != null){
            Gson gson = new Gson();
            ItemInfo itemInfo = gson.fromJson(itemInfoStr,ItemInfo.class);
            progressBar1.setProgress(progressBar1.getProgress()+itemInfo.getHp());
            progressBar2.setProgress(progressBar2.getProgress()+itemInfo.getAttack());
            progressBar3.setProgress(progressBar3.getProgress()+itemInfo.getSpeed());
        }
    }
}
